package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.user.*;
import com.dongdongshop.pojo.vo.AddressVo;
import com.dongdongshop.service.user.AddressService;
import com.dongdongshop.service.user.AreasService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("address")
public class UserHomeController {

    private final Logger logger = LoggerFactory.getLogger(UserHomeController.class);

    @Reference
    private AddressService addressServiceI;

    @Reference
    private AreasService areasService;

    /**
     * 跳转用户后台
     */
    @RequestMapping("toUserHome")
    public String toUserHome(){
        return "userhome/home-index";
    }

    /*
    * 根据用户主体
    * 查询我的所有地址
    * */
    @RequestMapping("getAddressList")
    @ResponseBody
    public Result getAddressList(){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            List<AddressVo> voAddresses = addressServiceI.getAddressList(user.getUsername());
            return Result.response(ResultEnum.SUCCESS).setData(voAddresses);
        }catch (Exception e){
            logger.error("根据用户Id查询地址管理: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 增加地址
    * */
    @RequestMapping("insertAddress")
    @ResponseBody
    public Result insertAddress(TbAddress address){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            address.setIsDefault("0");
            address.setCreateDate(new Date());
            //添加地址
            addressServiceI.insertAddress(address);
            return Result.response(ResultEnum.SUCCESS).setData("1");
        }catch (Exception e){
            logger.error("增加地址管理: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 删除地址
    * */
    @RequestMapping("deleteAddress")
    @ResponseBody
    public Result deleteAddress(Integer id){
        try {
            addressServiceI.deleteAddress(id);
            return Result.response(ResultEnum.SUCCESS).setData("1");
        }catch (Exception e){
            logger.error("根据Id查询删除地址: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 修改默认地址
    * */
    @RequestMapping("updateIsDefault")
    @ResponseBody
    public Result updateIsDefault(Integer id){
        try {
            addressServiceI.updateIsDefault(id);
            return Result.response(ResultEnum.SUCCESS).setData("1");
        }catch (Exception e){
            logger.error("修改默认地址失败: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 查寻省级地域
    * */
    @RequestMapping("/getProvinceList")
    @ResponseBody
    public Result getAddressList1(){
        try {
            List<TbProvinces> provinces = areasService.getAddressList();
            return Result.response(ResultEnum.SUCCESS).setData(provinces);
        }catch (Exception e){
            logger.error("查询省: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 查寻市级地域
    * */
    @RequestMapping("/getCitiesList")
    @ResponseBody
    public Result getCitiesList(String provinceid){
        try {
            //根据省的id查询市
            List<TbCities> cities = areasService.getCitiesList(provinceid);
            return Result.response(ResultEnum.SUCCESS).setData(cities);
        }catch (Exception e){
            logger.error("查询市: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    /*
    * 查询县级地址
    * */
    @RequestMapping("/getAreaList")
    @ResponseBody
    public Result getAreaList(String cityid){
        try {
            //根据省的id查询市
            List<TbAreas> cities = areasService.getAreaList(cityid);
            return Result.response(ResultEnum.SUCCESS).setData(cities);
        }catch (Exception e){
            logger.error("查询市: {}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
}
